<?php
class UploadAction extends CommonAction{
	//首页
	public function index(){
		C('SHOW_PAGE_TRACE',false);		//不显示调试信息
		$uploadfield	= $_REQUEST['uploadfield'];	//上传文件
		$thumbfield	= $_REQUEST['thumbfield'];	//缩略图
		$preview		= $_REQUEST['preview'];	//预览区域
		$this->assign("uploadfield",$uploadfield);
		$this->assign("thumbfield",$thumbfield);
		$this->assign("preview",$preview);
		$this->display();
	}

	/*
	* 处理图片上传
	*/
	public function doupload(){
		header("Content-Type:'text/html;Charset=utf-8'");
		if ($_FILES['uploadfile']['name']){
			import("ORG.Net.UploadFile");
			$uid	= getUid();
			$upload = new UploadFile(); // 实例化上传类
			//设置上传格式
			$upload->maxSize  = '3145728'; // 设置附件上传大小
			$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
			$upload->savePath =  $_SERVER['DOCUMENT_ROOT'].'/uploads/user/'. $uid .'/'; // 设置附件上传目录
			$upload->saveRule = 'time';//文件命名规则
			$upload->autoSub = true;//子目录存放
			$upload->subType = 'date';//子目录格式
			$upload->dateFormat = 'Y/m';//日期目录格式

			if(!$upload->upload()) { // 上传错误提示错误信息
				$this->error($upload->getErrorMsg());
			}else{ // 上传成功获取上传文件信息
				$file_info =  $upload->getUploadFileInfo();
			}
			//组装图片url
			$imgurl = '/uploads/user/'. $uid .'/'.$file_info[0]['savename'];
			//前页保存字段
			$uploadfield = $_REQUEST['uploadfield'] ? $_REQUEST['uploadfield'] : 'picture';
			//生成缩略图
			import("ORG.Util.Image");
			$image		= new Image();
			$thumbfield	= $_REQUEST['thumbfield'] ? $_REQUEST['thumbfield'] : 'thumb';
			$preview		= $_REQUEST['preview'] ? $_REQUEST['preview'] : 'preview';
			$thumb = $image->thumb($file_info[0]['savepath'].$file_info[0]['savename'],$file_info[0]['savepath'].$file_info[0]['savename'].'_thumb.jpg','',300,200);
			$thumb = str_replace($file_info[0]['savepath'],'/uploads/user/'.$uid .'/',$thumb);
			if($file_info){
				echo '
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
				<style>body{padding:0;margin:0;background:#F7F8F9;font-size:12px;line-height:26px;}</style>
				上传成功
				';
				if($_REQUEST['uploadfield']){echo  "<script language='javascript'>parent.document.getElementById(\"$uploadfield\").value=\"$imgurl\";</script>";}
				if($_REQUEST['thumbfield']){echo  "<script language='javascript'>parent.document.getElementById(\"$thumbfield\").value=\"$thumb\";</script>";}
				if($_REQUEST['preview']){echo  "<script language='javascript'>parent.document.getElementById(\"$preview\").innerHTML=\"<img src='$thumb'>\";</script>";}
			}
		}
	}
}
?>

